﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using System.Configuration;
using DataTransfer;

namespace BUS
{
   public class HoaDonBus
    {
        public string connect = QuiDinhBus.connect;
        public static QLPMDataContext db;
        // db = new QLPMDataContext(connect);
        public IEnumerable<HoaDon> GetAll()
        {
            IEnumerable<HoaDon> result = null;
            try
            {
                db = new QLPMDataContext(connect);
                result = from t in db.HoaDons select t;
                //db.Dispose();

            }
            catch (Exception)
            {

                throw;
            }
            return result;


        }
        public int Insert(HoaDon info)
        {
            int id = -1;
            try
            {
                db = new QLPMDataContext(connect);
                db.HoaDons.InsertOnSubmit(info);
                db.SubmitChanges();

                id = info.ID;
            }
            catch (Exception)
            {

                throw;
            }

            return id;
        }
        public HoaDon Get(int key)
        {
            HoaDon info = new HoaDon();
            info = null;
            try
            {
                db = new QLPMDataContext(connect);
                info = (from qd in db.HoaDons where qd.ID == key select qd).SingleOrDefault();
            }
            catch (Exception)
            {

                throw;
            }
            return info;
        }
        public bool Delele(int key)
        {
            bool flag = false;
            try
            {

                db = new QLPMDataContext(connect);

                HoaDon del = (from t in db.HoaDons where t.ID == key select t).SingleOrDefault();

                db.HoaDons.DeleteOnSubmit(del);

                db.SubmitChanges();

                flag = true;
            }
            catch (Exception)
            {

                throw;
            }
            return flag;
        }
       
        public bool Update(HoaDon info)
        {
            bool flag = false;
            try
            {
                db = new QLPMDataContext(connect);
                var t = (from qd in db.HoaDons where qd.ID == info.ID select qd).SingleOrDefault();
                t.IDBenhNhan = info.IDBenhNhan;
                t.TienKham = info.TienKham;
                t.TienThuoc = info.TienThuoc;
                t.UserModify = info.UserModify;
                t.ModifyDate = info.ModifyDate;
                db.SubmitChanges();


                flag = true;

            }
            catch (Exception)
            {

                throw;
            }
            return flag;
        }
        public Double GetTienKham()
        {
            Double tienkham=0;
            try
            {
                db = new QLPMDataContext(connect);
                
                QuiDinh qd = (from t in db.QuiDinhs where t.QD == "TienKham" select t).SingleOrDefault();
                if (qd != null)
                {
                    tienkham =(Double) qd.Value * 1000;
                }
                   
                
            }
            catch (Exception)
            {

                throw;
            }
                return tienkham;
        }
        public IEnumerable<object> joinTable(BenhNhan info)
        {
            IEnumerable<object> result=null;
            try
            {
                db = new QLPMDataContext(connect);
                 result = from bn in db.BenhNhans
                             join pk in db.PhieuKhams on bn.IDPhieuKham equals pk.ID
                             join pkct in db.PhieuKhamChiTiets on bn.IDPhieuKham equals pkct.IDPhieuKham
                             join thuoc in db.Thuocs on pkct.IDTHuoc equals thuoc.ID
                             where bn.ID == info.ID
                             select new { thuoc.GiaBan };
                 

            }
            catch (Exception)
            {

                throw;
            }
            return result;
        }
        public Double GetTienThuoc(BenhNhan info)
        {
            Double tienthuoc = 0;
            try
            {
                db = new QLPMDataContext(connect);
                var result = from bn in db.BenhNhans
                             join pk in db.PhieuKhams on bn.IDPhieuKham equals pk.ID
                             join pkct in db.PhieuKhamChiTiets on bn.IDPhieuKham equals pkct.IDPhieuKham
                             join thuoc in db.Thuocs on pkct.IDTHuoc equals thuoc.ID
                             where bn.ID == info.ID
                             select new {thuoc.GiaBan };
                foreach (var item in result)
                {
                    tienthuoc += (double)item.GiaBan;
                }
                tienthuoc = tienthuoc * 1000;
            }
            catch (Exception)
            {

                throw;
            }
            return tienthuoc;
        }
        public bool LapHoaDon(BenhNhan bn,User user)
        {
                    try 
	        {	        
		        HoaDon hdon = new HoaDon();
                    hdon.IDBenhNhan = bn.ID;
                    hdon.TienKham = GetTienKham();
                    hdon.TienThuoc = GetTienThuoc(bn);
                    hdon.UserCreate = user.UserName;
                    hdon.CreateDate = DateTime.Now;
                    HoaDonBus hdonBus = new HoaDonBus();
                    hdonBus.Insert(hdon);
                    return true;
	        }
	        catch (Exception)
	        {
		
		        throw;
	        }

                    return false;
        }
    }
}
